package com.zzh.elasticSearchDemo.originalESConnect.impl;/**
 * @ClassName OptionServiecImpl
 * @date 2021/3/15 20:43
 * @Version 1.0
 * @Description //TODO
 */

import com.alibaba.fastjson.JSONObject;
import com.zzh.elasticSearchDemo.model.JobDetail;
import com.zzh.elasticSearchDemo.originalESConnect.OptionImpl;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 *@ClassName OptionServiecImpl
 *@date 2021/3/15 20:43
 *@Version 1.0
 *@Description //
 */
public class OptionServiecImpl implements OptionImpl {
    private RestHighLevelClient restHighLevelClient;

    public OptionServiecImpl() {
        // 建立与ES的连接
        // 1. 使用RestHighLevelClient构建客户端连接。
        // 2. 基于RestClient.builder方法来构建RestClientBuilder
        // 3. 用HttpHost来添加ES的节点
        RestClientBuilder restClientBuilder = RestClient.builder(
                new HttpHost("192.168.44.132", 9200, "http")
//                , new HttpHost("192.168.21.131", 9200, "http")
//                , new HttpHost("192.168.21.132", 9200, "http")
        );
        restHighLevelClient = new RestHighLevelClient(restClientBuilder);
    }
    // 索引库的名字
    private static final String JOB_IDX = "job_index";

    @Override
    public void add(JobDetail jobDetail) throws IOException {
//1.	构建IndexRequest对象，用来描述ES发起请求的数据。
        IndexRequest indexRequest = new IndexRequest(JOB_IDX);


        //2.	设置文档ID。
        indexRequest.id(jobDetail.getId() + "");

        //3.	使用FastJSON将实体类对象转换为JSON。
        String json = JSONObject.toJSONString(jobDetail);

        //4.	使用IndexRequest.source方法设置文档数据，并设置请求的数据为JSON格式。
        indexRequest.source(json, XContentType.JSON);

        //5.	使用ES High level client调用index方法发起请求，将一个文档添加到索引中。
        restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
    }

    @Override
    public JobDetail findById(long id) throws IOException {
        // 1.	构建GetRequest请求。
        GetRequest getRequest = new GetRequest(JOB_IDX, id + "");

        // 2.	使用RestHighLevelClient.get发送GetRequest请求，并获取到ES服务器的响应。
        GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);

        // 3.	将ES响应的数据转换为JSON字符串
        String json = getResponse.getSourceAsString();

        // 4.	并使用FastJSON将JSON字符串转换为JobDetail类对象
        JobDetail jobDetail = JSONObject.parseObject(json, JobDetail.class);

        // 5.	记得：单独设置ID
        jobDetail.setId(id);

        return jobDetail;
    }

    @Override
    public void update(JobDetail jobDetail) throws IOException {
        GetRequest getRequest = new GetRequest(JOB_IDX,jobDetail.getId()+"");
        Boolean exits = restHighLevelClient.exists(getRequest,RequestOptions.DEFAULT);
        if (exits) {
            UpdateRequest request = new UpdateRequest(JOB_IDX,jobDetail.getId()+"");
            request.doc(JSONObject.toJSONString(jobDetail),XContentType.JSON);
            restHighLevelClient.update(request,RequestOptions.DEFAULT);

        }
    }

    @Override
    public void close() throws IOException {
        restHighLevelClient.close();
    }

}
